<template>
    <!-- 经销商管理 -->
    <el-dialog :title="`${ruleForm.view ? '查看' : ruleForm.id ? '编辑' : '新增'}经销商`" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form v-loading="pageLoading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template lable="基本信息">
                <el-col :span="12">
                    <el-form-item label="经销商名称" prop="name">
                        <div class="flex-y-center">
                            <el-input :disabled="ruleForm.view" v-model="ruleForm.name" placeholder="请输入经销商名称"></el-input>
                            <el-button class="ml-lg" v-if="!ruleForm.view" size="medium" type="primary" @click="handleDealerInfo">查询</el-button>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="企业类型" prop="type">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.type" placeholder="请输入企业类型"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="统一社会信用代码" prop="creditno">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.creditno" placeholder="请输入统一社会信用代码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="注册地址" prop="addressDetails">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.addressDetails" placeholder="请输入注册地址"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="法人" prop="legal">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.legal" placeholder="请输入法人"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="注册资本" prop="registerCapital">
                        <c-number-input :disabled="ruleForm.view" v-model="ruleForm.registerCapital" placeholder="请输入注册资本">
                            <template slot="append"> 万元 </template>
                        </c-number-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="成立日期" prop="establishDate">
                        <el-date-picker class="wh" :disabled="ruleForm.view" v-model="ruleForm.establishDate" type="date" placeholder="选择日期" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="经营范围" prop="businessScope">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.businessScope" placeholder="请输入经营范围"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机号" prop="mobile">
                        <el-input :disabled="!!ruleForm.id" v-model="ruleForm.mobile" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="dialogVisible">
                    <el-form-item label="营业执照" prop="certificatePicPath">
                        <c-upload :disabled="ruleForm.view" @change="$refs.ruleForm.validateField('certificatePicPath')" v-model="ruleForm.certificatePicPath" listType="text" :limit="1" :accept="['.jpg','.png']" :tip="['只能上传jpg/png文件，且不超过500kb']" />
                    </el-form-item>
                </el-col>
            </c-form-template>
            <!-- 查看用到 -->
            <c-form-template lable="会员信息" v-if="ruleForm.view">
                <el-col :span="12">
                    <el-form-item label="会员等级" prop="grade">
                        <el-input :disabled="true" v-model="ruleForm.memberInfo.grade"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="当前会员金额" prop="currentAmount">
                        <c-money-input :disabled="true" v-model="ruleForm.dealerAmount.currentAmount"><template slot="append"> 元 </template></c-money-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="会员有效期" prop="timeExpiry">
                        <el-input :disabled="true" :value="dateYMDHMSFormat(ruleForm.dealerAmount.timeExpiry)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="优惠比例" prop="discountRatio">
                        <el-input :disabled="true" v-model="ruleForm.memberInfo.discountRatio"><template slot="append"> % </template></el-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
            <c-form-template lable="会员充值记录" class="mr-lg ml-lg" v-if="
          ruleForm.view &&
          ruleForm.dealerAmountLog &&
          ruleForm.dealerAmountLog.length > 0
        ">
                <el-table :header-cell-style="{ background: '#F1F1F1', color: '#092132' }" :data="ruleForm.dealerAmountLog">
                    <el-table-column label="序号" align="center" width="80" type="index" />
                    <el-table-column label="充值金额" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.amount | thousands }}
                        </template>
                    </el-table-column>
                    <el-table-column label="可用余额" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.postAmount | thousands }}
                        </template>
                    </el-table-column>
                    <el-table-column label="充值日期" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.timeCreate | dateYMDHMSFormat }}
                        </template>
                    </el-table-column>
                </el-table>
            </c-form-template>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button v-show="!ruleForm.view" :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    data () {
        return {
            dialogVisible: false,
            loading: false,
            pageLoading: false,
            ruleForm: {
                memberInfo: {},
                dealerAmount: {},
                certificatePicPath: [],
            },
            rules: {
                name: [
                    { required: true, message: "请输入经销商名称", trigger: "blur" },
                ],
                addressDetails: [
                    {
                        required: true,
                        message: "请输入注册地址",
                        trigger: "blur",
                    },
                ],
                creditno: [
                    {
                        required: true,
                        message: "请输入统一社会信用代码",
                        trigger: "blur",
                    },
                ],
                legal: [{ required: true, message: "请输入法人", trigger: "blur" }],
                registerCapital: [
                    { required: true, message: "请输入注册资本", trigger: "blur" },
                ],
                establishDate: [
                    { required: true, message: "请选择成立日期", trigger: "change" },
                ],
                mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
                certificatePicPath: [
                    {

                        required: true,
                        message: "请上传营业执照",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    methods: {

        handleDealerInfo () {
            if (!this.ruleForm.name) {
                this.$message.warning("请填写经销商名称");
                return;
            }
            this.$get({
                url: "/DealerAmount/getBusinesssInfoByKeyword",
                params: {
                    keyword: this.ruleForm.name,
                },
            }).then((res) => {
                const fields = [
                    "name",
                    "type",
                    "creditno",
                    "legal",
                    "registerCapital",
                    "addressDetails",
                    "establishDate",
                    "businessScope",
                    "mobile",
                    "certificatePicPath",
                ];
                for (let i = 0; i < fields.length; i++) {
                    if (res[fields[i]]) {
                        this.$set(this.ruleForm, fields[i], res[fields[i]]);
                    }
                }
            });
        },
        handleSubmit () {
            console.log(this.ruleForm);
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        this.$post({
                            url: `/DealerAmount/${data.id ? "update" : "add"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {
            this.dialogVisible = true;
            if (item && item.id) {
                const params = {
                    id: item.id,
                };
                this.pageLoading = true;
                this.$get({ url: "/DealerAmount/getById", params }).then((res) => {
                    // console.log(this.ruleForm);
                    this.pageLoading = false;
                    this.$get({ url: "/DealerAmount/getByAmountLong", params }).then(
                        (amount) => {
                            this.ruleForm = {
                                ...res.dealerInfo,
                                dealerAmountLog: amount || [],
                                dealerAmount: res.dealerAmount || {},
                                memberInfo: res.memberInfo || {},
                                view: item.view || false,
                            };
                            this.pageLoading = false;
                        }
                    );
                });
            }
        },
        handleClose () {
            this.ruleForm = {
                address: [],
                dealerAmount: {},
                memberInfo: {},
            };
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
